import ErrorComp from '@/components/common/error-comp';
import NewMessageNotify from '@/components/common/new-message-notify';
import UserHeader from '@/components/user/user-header';
import env from '@/lib/env';
import SocketProvider from '@/providers/socket-provider';
import { getNewMessagesNotify } from '@/service/query/message';

export const dynamic = 'force-dynamic';

export default async function Layout({
  children,
}: { children: React.ReactNode }) {
  const { result, error } = await getNewMessagesNotify();
  if (error) return <ErrorComp message={error.message} />;

  return (
    <SocketProvider appUrl={env.APP_URL}>
      <NewMessageNotify offlineMessage={result} />
      <div className="bg-muted">
        <UserHeader />
        <div className="max-w-[850px] mx-auto bg-background min-h-screen">
          {children}
        </div>
      </div>
    </SocketProvider>
  );
}
